import request from './request'
import { ReminderForm } from '@/views/order/list/components/Reminder/types'
import { TimeoutTipForm } from '@/views/order/list/components/timeoutTip/types'
import { OrderAPIRequestParam } from '@/typings/api/order'
import axios, { AxiosPromise } from 'axios'

const cancelToken = axios.CancelToken
let cancel = () => {}
// 创建或修改工单保存
export function addSave(data: {
  content: string
  createTime: any
  delImageIds: any[]
  delOrderGoodsIds: any[]
  empId: any
  id: any
  imageVos: any[]
  moduleId: any
  networkId: any
  orderGoodsTypes: any[]
  orderUser: {
    address: string
    area: any
    city: any
    name: string
    otherPhone: string
    phone: string
    province: any
    town: any
    userId: any
  }
  overinsured: any
  source: number
  threeTypeId: any
  threeWayId: any
  twoTypeId: any
  twoWayId: any
  oneTypeId: any
  oneWayId: any
  workLevelId: any
  workFieldVal: any[]
}) {
  return request({
    url: '/work/order/save',
    method: 'post',
    data,
  })
}

/**
 * 工单模块下拉列表查询
 * @param data - 请求参数
 * @param data.factoryId - 品牌商家id
 * @param data.limit - 每页显示数量
 * @param data.name - 搜索关键词
 * @param data.page - 当面页码
 */
export function workModulePageSelect(data: {
  limit: number
  name: string
  page: number
  factoryId: string
}): AxiosPromise<APIResponseList<OrderAPIResponse.OrderModuleOptionListItem>> {
  return request({
    url: '/order/select/workModulePageSelect',
    method: 'post',
    data,
  })
}

// 工单级别下拉列表查询
export function workLevelPageSelect(data: { limit: number; name: string; page: number }) {
  return request({
    url: '/order/select/workLevelPageSelect',
    method: 'post',
    data,
  })
}
// 工单客服组下拉列表查询
export function workTeamPageSelect(data: { limit: number; name: string; page: number }) {
  return request({
    url: '/order/select/workTeamPageSelect',
    method: 'post',
    data,
  })
}
// 用户联系人下拉数据列表
export function getUserSelect(data: { limit: number; name: string; page: number }) {
  return request({
    url: '/select/user/pageUserSelect',
    method: 'post',
    data,
  })
}
// 用户联系人下拉数据列表
export function userSelect(data: { limit: number; name: string; page: number }, userId: string) {
  return request({
    url: '/select/user/' + userId,
    method: 'post',
    data,
  })
}
// 获取工单报单产品
export function getOrderProduct(data: {
  brandId: string
  keyword: string
  limit: number
  page: number
  typeId: string
}) {
  return request({
    url: '/goods/product/getOrderProduct',
    method: 'post',
    data,
  })
}
// 工单模块处理方式下拉列表查询
export function workModuleWayPageSelect(
  data: { limit: number; name: string; page: number },
  moduleId: string,
) {
  return request({
    url: '/order/select/workModuleWayPageSelect/' + moduleId,
    method: 'post',
    data,
  })
}

// 用户搜索
export function SearchCustomerInfo(
  data: { limit: number; name: string; page: number },
  moduleId: string,
) {
  return request({
    url: '/user/contact/selectName',
    method: 'post',
    data,
  })
}
//工单模块分类下拉列表查询
export function workModuleTypePageSelect(
  data: { limit: number; name: string; page: number },
  moduleId: string,
) {
  return request({
    url: '/order/select/workModuleTypePageSelect/' + moduleId,
    method: 'post',
    data,
  })
}

/**
 * 获取工单列表
 * @param data - 请求参数
 * @param data.addresses - 地址集合
 * @param data.area - 镇区
 * @param data.areas - 镇区
 * @param data.childStatus - 子状态：待服务商处理：1-服务商未接单，2-服务商未派单，3-工程师未接单；处理中：1-未拨打电话，2-未预约，3-未签到，4-费用待审核，5-配件待审核
 * @param data.city - 市
 * @param data.citys - 市
 * @param data.dateFieldType - 时间字段类型：1-报单日期，2-完工日期，3-结算日期
 * @param data.dateType - 时间类型：1-时间区间，2-今天，3-本周，4-本月，5-过去三个月
 * @param data.empIds - 客服ID
 * @param data.empType - 客服类型：1-已分配客服，0-未分配客服，传空值则查询所有
 * @param data.factoryIds - 商家id
 * @param data.fieldId - 工单字段ID
 * @param data.fieldSearch - 工单字段搜索关键词
 * @param data.goodsSearch - 商品搜索：品牌/产品名称/产品型号
 * @param data.isHs - 是否待核实：为1是待核实
 * @param data.isHx - 是否待核销：为1是待核销
 * @param data.isPause - 是否暂挂
 * @param data.limit - 每页显示数量
 * @param data.maxServiceTime - 最大服务时长值
 * @param data.minServiceTime - 最小服务时长值
 * @param data.moduleIds - 工单模块ID
 * @param data.networkIds - 服务区域ID
 * @param data.networkType - 网点类型：1-已派单，0-未派单，传空值则查询所有
 * @param data.orderEndTime - 报单结束时间
 * @param data.orderNo - 工单单号
 * @param data.orderNodeTimeoutType - 工单节点时效类型：0-未超时工单，1-已超时工单
 * @param data.orderStartTime - 报单开始时间
 * @param data.orderUserSearch - 工单客户搜索关键词：客户名称/客户手机号/客户地址
 * @param data.page - 当面页码
 * @param data.province - 省
 * @param data.provinces - 省
 * @param data.serviceTimeType - 服务时长类型：1-小时，2-天
 * @param data.sources - 工单来源
 * @param data.statues - 工单状态：1-待派单，2-待服务商确认，3-处理中，4-已完成，5-已作废，1001-结单审核
 * @param data.status - 工单状态：1-待派单，2-待服务商确认，3-处理中，4-已完成，5-已作废，1001-结单审核
 * @param data.workLevelIds - 等级ID
 */
export function getOrderList(
  data: OrderRequestParam.OrderList,
): AxiosPromise<APIResponseList<OrderAPIResponse.OrderListItem>> {
  return request({
    url: '/work/order/getList',
    method: 'post',
    data,
  })
}

/**
 * 获取工单列表状态数量
 */
export function getOrderStatusQty(): AxiosPromise<
  APIResponse<OrderAPIResponse.OrderListStatusQty>
> {
  return request({
    url: '/work/order/getOrderStatusQty',
    method: 'get',
  })
}

/**
 * 根据工单ID获取工单详情
 * @param {string} id 工单id
 */
export function getOrderDetail(id: string) {
  return request({
    url: '/work/order/getOrderDetail/' + id,
    method: 'get',
  })
}

//根据模块ID获取工单自定义字段
export function getModuleField(id: string) {
  return request({
    url: '/work/module/field/getModuleField/' + id,
    method: 'get',
  })
}
//获取工单列表搜索条件的工单自定义搜索字段
export function getOrderListSelectField() {
  return request({
    url: '/work/order/getOrderListSelectField',
    method: 'get',
  })
}
// 批量分配客服
export function dispatchOrderEmp(data: unknown[], empId: string) {
  return request({
    url: '/work/order/dispatchOrderEmp/' + empId,
    method: 'put',
    data,
  })
}
// 批量更新工单流程为完成
export function finishOrder(data: { remark: string; workIds: unknown[] }) {
  return request({
    url: '/work/order/finishOrder',
    method: 'put',
    data,
  })
}

//批量更新工单流程为取消
export function cancelOrder(data: {
  cancelReason: 2 | 3
  remark: string
  workIds: string[]
  approach: 1 | 2 | 3 | 4
}): AxiosPromise<APIResponse> {
  return request({
    url: '/work/order/cancelOrder',
    method: 'put',
    data,
  })
}

//批量更新工单流程为挂起/解除
export function peddingOrder(data: { id: string }): AxiosPromise<APIResponse> {
  return request({
    url: '/work/order/updateInfo',
    method: 'post',
    data,
  })
}
/**
 * 工单催单
 * @param {string} data.content 催单内容
 * @param {Array<string>} data.workIds 工单ID集合
 * @param {Array<number>} data.remindEmpType 提醒人员类型：1-工单客服，2-服务商负责人，3-工程师
 */
export function saveReminder(data: ReminderForm) {
  return request({
    url: '/work/order/saveReminder',
    method: 'post',
    data,
  })
}

// 根据工单ID获取工单催单列表
export function getReminderList(data: { limit: number; page: number }, workId: string | unknown[]) {
  return request({
    url: '/work/order/getReminderList/' + workId,
    method: 'post',
    data,
  })
}

/**
 * 根据工单ID获取工单商品
 * @param {string} workId 工单id
 * @param {Object} data 分页配置
 * @param {number} data.limit 一页显示多少条
 * @param {number} data.page 页码
 */
export function getOrderGoodsDetail(workId: string, data: { limit: number; page: number }) {
  return request({
    url: '/work/order/getOrderGoodsDetail/' + workId,
    method: 'post',
    data,
  })
}

//根据工单ID获取工单操作记录列表
export function getOrderLog(data: { limit: number; page: number }, workId: string) {
  return request({
    url: '/work/order/getOrderLog/' + workId,
    method: 'post',
    data,
  })
}
// 工单预约时间
export function reservationTime(data: {
  date: string
  remark: string
  time: string
  workId: string
}) {
  return request({
    url: '/work/order/reservationTime',
    method: 'put',
    data,
  })
}
// 工单派单给服务商
export function orderDispatchNetwork(data: {
  networkEmpId?: string
  networkId: string
  remark?: string
  workIds: Array<string>
}) {
  return request({
    url: '/work/order/orderDispatchNetwork',
    method: 'put',
    data,
  })
}
//根据工单ID获取工单留言
export function getWorkLeaveMessageList(data: { limit: number; page: number }, workId: string) {
  return request({
    url: '/work/order/getWorkLeaveMessageList/' + workId,
    method: 'post',
    data,
  })
}
// 保存工单留言
export function saveWorkLeaveMessage(data: {
  annex: any[]
  content: string
  visitTypes: any
  workId: string | unknown[]
}) {
  return request({
    url: '/work/order/saveWorkLeaveMessage',
    method: 'post',
    data,
  })
}
// 改派服务商
export function changeNetwork(data: {
  changeToNetworkEmpId: any
  changeToNetworkId: any
  currentNetworkId: string
  remark: string
  workId: unknown
}) {
  return request({
    url: '/work/order/changeNetwork',
    method: 'put',
    data,
  })
}
// 改派工单客服
export function changeServiceEmp(data: { empId: string; remark: string; workId: unknown }) {
  return request({
    url: '/work/order/changeServiceEmp',
    method: 'put',
    data,
  })
}

/**
 * 根据工单ID获取服务商信息
 * 以后此接口会废弃，所以不建议使用
 * @param {string | Array<string>} workId 工单id
 * @returns {Promise<any>}
 */
export function getServiceInfo(workId: string) {
  return request({
    url: '/work/order/getServiceInfo/' + workId,
    method: 'get',
  })
}

// 获取工单列表表头的工单自定义字段
export function getOrderListTableTitleField() {
  return request({
    url: '/work/order/getOrderListTableTitleField',
    method: 'get',
  })
}
// 根据工单ID获取服务项目列表
export function getOrderDetailFeeProjectList(data: any, workId: string | string[]) {
  return request({
    url: '/work/fee/getOrderDetailFeeProjectList/' + workId,
    method: 'post',
    data,
  })
}
// 获取工单服务费用项目列表
export function feeWork(data: {
  applyEmp: string
  createEndTime: string
  createStartTime: string
  feeNo: string
  limit: number
  page: number
  status: any
  workNo: string
}) {
  return request({
    url: '/work/fee/getList',
    method: 'post',
    data,
  })
}
// 根据服务项目ID获取项目信息列表
export function OrderFeeDetailProjectInfo(id: string) {
  return request({
    url: '/work/fee/getOrderFeeDetailProjectInfo/' + id,
    method: 'get',
  })
}
// 审核工单服务费用项目
export function checkOrderFee(data) {
  return request({
    url: '/work/fee/checkOrderFee',
    method: 'post',
    data,
  })
}
// 保存或修改工单服务费用
export function feeSaveOrder(data) {
  return request({
    url: '/work/fee/save',
    method: 'post',
    data,
  })
}
// 工单费用项目下拉列表查询
export function workFeeProjectPageSelect(data) {
  return request({
    url: '/order/select/workFeeProjectPageSelect',
    method: 'post',
    data,
  })
}
// 根据服务项目ID获取服务项目详情
export function getOrderFeeDetail(id: string) {
  return request({
    url: '/work/fee/getOrderFeeDetail/' + id,
    method: 'get',
  })
}
// 根据服务项目ID获取服费用项目日志列表
export function getOrderFeeLogList(data, id: string) {
  return request({
    url: '/work/fee/getOrderFeeLogList/' + id,
    method: 'post',
    data,
  })
}
// 分页获取工单配件下拉数据列表
export function getOrderSparePageSelect(data, workId: string) {
  return request({
    url: '/order/select/getOrderSparePageSelect/' + workId,
    method: 'post',
    data,
  })
}
// 分页获取工单配件规格下拉数据列表
export function getOrderSpareSpecSelect(data, spareId: string) {
  return request({
    url: '/order/select/getOrderSpareSpecSelect/' + spareId,
    method: 'post',
    data,
  })
}

/**
 * 根据工单ID获取工单信息
 * 以后会废弃，所以此接口不建议使用
 * @param {string} workId 工单id
 * @returns {Promise<any>}
 */
export function getOrderParamField(workId: string) {
  return request({
    url: '/work/order/getOrderParamField/' + workId,
    method: 'get',
  })
}

// 获取工单列表表头字段
export function getOrderListTableField(type) {
  return request({
    url: '/workTableField/getOrderListTableField/' + type,
    method: 'get',
  })
}
// 获取工单表头字段设置项
export function getOrderTableField(type) {
  return request({
    url: '/workTableField/getOrderTableField/' + type,
    method: 'get',
  })
}

/**
 * 保存或更新工单表头字段
 * @param {Array} data - 请求参数
 * @param {1 | 2 | 3} type - 表头类型：1-工单列表，2-存档列表，3-结算列表
 */
export function workTableFieldSave(data: Array<any>, type: 1 | 2 | 3) {
  return request({
    url: `/workTableField/save/${type}`,
    method: 'post',
    data,
  })
}

// 获取工单列表排序字段设置
export function getOrderTableOrderByField(type) {
  return request({
    url: '/orderBy/field/getOrderTableOrderByField/' + type,
    method: 'get',
  })
}
// 新增或修改工单列表排序字段
export function orderBySave(data, type) {
  return request({
    url: '/orderBy/field/save/' + type,
    method: 'post',
    data,
  })
}

//获取工单表头初始化字段
export function getOrderTableInitField() {
  return request({
    url: '/workTableField/getOrderTableInitField',
    method: 'get',
  })
}
//获取工单列表排序初始化字字段
export function getOrderTableInitOrderByField() {
  return request({
    url: '/orderBy/field/getOrderTableInitOrderByField',
    method: 'get',
  })
}

/**
 * 工单模块带费用下拉列表查询
 * @param {number} data.limit 每页显示数量
 * @param {string} data.name 搜索关键词
 * @param {number} data.page 当面页码
 * @returns {Promise<any>}
 */
export function workModulePricePageSelect(data: { limit: number; name: string; page: number }) {
  return request({
    url: '/order/select/workModulePricePageSelect',
    method: 'post',
    data,
  })
}

// 获取工单所有导出字段
export function getOrderExportField() {
  return request({
    url: '/work/export/field/getOrderExportField',
    method: 'get',
  })
}

// 获取工单自定义字段
export function getOrderCustomField() {
  return request({
    url: '/work/export/field/getOrderCustomField',
    method: 'get',
  })
}

// 获取工单系统初始导出字段
export function getOrderSysExportFields() {
  return request({
    url: '/work/export/field/getOrderSysExportFields',
    method: 'get',
  })
}

// 保存导出字段
export function saveExportField(data) {
  return request({
    url: '/work/export/field/save',
    method: 'post',
    data,
  })
}

// 保存并导出字段
export function saveAndExportField(data) {
  return request({
    url: '/work/export/field/saveAndExportField',
    method: 'post',
    data,
    responseType: 'blob',
  })
}

// 获取工单详情的合计总费用
export function getOrderTotalFee(id: string) {
  return request({
    url: `/work/order/getOrderTotalFee/${id}`,
    method: 'get',
  })
}

/**
 * 保存工单备注
 * @param {object} data - 请求参数
 * @param {Array<any>} data.annexs - 工单备注附件
 * @param {string} data.remark - 工单备注
 * @param {0 | 1 | 2 | 3 | 4} data.visitTypes - 可见类型：0-公开，1-服务商，2-工程师，3-工单客户，4-工单客服
 * @param {Array<string>} data.workIds - 工单ID参数
 */
export function saveRemark(data: OrderAPIRequestParam.SaveOrderRemark) {
  return request({
    url: '/ork/remark/save',
    method: 'post',
    data,
  })
}

/**
 * 发送结单核销码
 * @param {string} id 工单id
 * @returns {Promise<any>}
 */
export function sendOrderStatementWriteOffCode(id: string) {
  return request({
    url: `/work/order/sendOrderStatementWriteOffCode/${id}`,
    method: 'get',
  })
}

/**
 * 工单超时提醒
 * @param {Array<string>} data.remindEmpIds 指定人员ID
 * @param {Array<number>} data.remindEmpType 提醒人员类型：1-工单客服，2-服务商负责人，3-工程师，4-指定人员
 * @param {Array<string>} data.workIds 工单ID
 */
export function workTimeoutRemind(data: TimeoutTipForm) {
  return request({
    url: '/work/order/workTimeoutRemind',
    method: 'post',
    data,
  })
}

/**
 * 获取待审核的工单事件
 * @param {string} id 工单ID
 */
export function getWaitCheckOrderEvent(id: string) {
  return request({
    url: `/work/order/getWaitCheckOrderEvent/${id}`,
    method: 'get',
  })
}
// 获取工单催单数量
export function getOrderReminderCount(id: string) {
  return request({
    url: `/work/order/getOrderReminderCount/${id}`,
    method: 'get',
  })
}
// 获取工单最新进展
export function getOrderLastLog(id: string) {
  return request({
    url: `/work/order/getOrderLastLog/${id}`,
    method: 'get',
    cancelToken: new cancelToken((c) => {
      // 接收一个 cancel 函数作为参数
      cancel = c
    }),
  })
}
// 取消上一次请求获取工单最新进展
export function axiosCancel() {
  cancel()
}
/**
 * 更改结算价
 * @param {Object} data - 请求参数
 * @param {number} data.factorySettlementPrice - 商家结算价
 * @param {number} data.networkSettlementPrice - 服务商结算价
 * @param {Array<string>} data.workIds - 工单ID
 */
export function updateSettlementPrice(data: OrderAPIRequestParam.UpdateSettlementPrice) {
  return request({
    url: '/work/order/updateSettlementPrice',
    method: 'post',
    data,
  })
}

/**
 * 审核结单
 * @param {string} id - 工单id
 * @param {object} data - 请求参数
 * @param {1 | 2} data.checkType - 结单审核类型：1-审核通过，2-驳回
 * @param {string} data.id - 工单结单ID
 * @param {number} data.price - 结单总费用
 * @param {string} data.remark - 审核结单备注
 * @param {number} data.surePrice - 确认结单费用
 */
export function checkStatementOrder(id: string, data: OrderAPIRequestParam.CheckStatementOrder) {
  return request({
    url: `/work/order/checkStatementOrder/${id}`,
    method: 'post',
    data,
  })
}

/**
 * 根据工单ID获取工单待结单审核
 * @param {string} id - 工单id
 */
export function getWaitCheckStatementOrder(id: string) {
  return request({
    url: `/work/order/getWaitCheckStatementOrder/${id}`,
    method: 'get',
  })
}

/**
 * 根据工单ID获取待审核取消工单
 * @param {string} id - 工单id
 */
export function getWaitCheckCancelOrder(id: string) {
  return request({
    url: `/work/order/getWaitCheckCancelOrder/${id}`,
    method: 'get',
  })
}

/**
 * 审核取消工单
 * @param {string} orderId - 工单id
 * @param {object} data - 请求参数
 * @param {1 | 2} data.checkType - 取消工单审核类型：1-审核通过，2-驳回
 * @param {string} data.id - 取消工单ID
 * @param {string} data.remark - 审核备注
 */
export function checkCancelOrder(orderId: string, data: OrderAPIRequestParam.CheckCancelOrder) {
  return request({
    url: `/work/order/checkCancelOrder/${orderId}`,
    method: 'post',
    data,
  })
}

/**
 * 根据工单ID获取待审核改派服务商的工单
 * @param {string} id - 工单id
 */
export function getWaitCheckWorkReassign(id: string) {
  return request({
    url: `/work/order/getWaitCheckWorkReassign/${id}`,
    method: 'get',
  })
}

/**
 * 审核改派服务商的工单
 * @param {Object} data - 请求参数
 * @param {string} data.changeToNetworkId - 新服务商ID
 * @param {1 | 2} data.checkTypep - 审核类型：1-审核通过，2-驳回
 * @param {string} data.id - 改派工单ID
 * @param {string} data.networkEmpId - 新服务商工程师ID
 * @param {string} data.remark - 备注
 * @param {string} workId - 工单id
 */
export function checkWorkReassign(data: OrderAPIRequestParam.CheckWorkReassign, workId: string) {
  return request({
    url: `/work/order/checkWorkReassign/${workId}`,
    method: 'post',
    data,
  })
}

/**
 * 根据工单ID获取结单字段
 * @param workId - 工单id
 */
export function getOrderSettlementField(
  workId: string,
): AxiosPromise<APIResponseRows<OrderDetailAPIResponse.FinishOrderTemplateList>> {
  return request({
    url: `/work/order/getOrderSettlementField/${workId}`,
    method: 'get',
  })
}

// 获取结单字段导出
export function getOrderSettlementFieldExport() {
  return request({
    url: `/work/export/field/getOrderSettlementField`,
    method: 'get',
  })
}

// 修改工单
export function orderUpdateInfo(data) {
  return request({
    url: `/work/order/updateInfo`,
    method: 'post',
    data,
  })
}

// 复制工单信息
export function getOrderCopySave(data) {
  return request({
    url: `/work/order/copySave`,
    method: 'post',
    data,
  })
}

/**
 * 乐开工单推送
 * @param {string} orderId - 工单id
 */
export function lekaiOrderPush(orderId: string): AxiosPromise<APIResponse> {
  return request({
    url: `callback/${orderId}`,
    method: 'get',
  })
}

/**
 * 工单回退状态
 * @param orderId - 工单id
 */
export const orderRevertState = (orderId: string): AxiosPromise<APIResponse> => {
  return request({
    url: `/work/order/backStatementOrder/${orderId}`,
    method: 'post',
  })
}

/**
 * 核实上门时间
 * @param data - 请求参数
 * @param data.remark - 备注
 * @param data.verifiedVisitTime - 核实时间
 * @param data.workId - 工单id
 */
export const onSiteVisitTimeVerification = (data: {
  remark: string
  verifiedVisitTime: string
  workId: string
}): AxiosPromise<APIResponse> => {
  return request({
    url: '/work/order/workOrder/verifyVisitTime',
    method: 'post',
    data,
  })
}
